<template>
  <div class="app-container">
    <el-radio-group v-model="tab" @change="handleChangeTab" style="margin-bottom: 20px">
      <el-radio-button label="others" ty>其它</el-radio-button>
      <el-radio-button label="sleep">睡觉</el-radio-button>
    </el-radio-group>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="" prop="val">
        <el-input placeholder="请输入" v-model="queryParams.val" class="input-with-select" style="width: 400px">
          <el-select v-model="selectType" slot="prepend" placeholder="请选择" style="width: 130px">
            <el-option label="设备名称" value="name"></el-option>
            <el-option label="姓名" value="uname"></el-option>
            <el-option label="电话" value="uphone"></el-option>
            <el-option label="蓝牙Mac地址" value="deviceId"></el-option>
          </el-select>
        </el-input>
      </el-form-item>
      <el-form-item label="" prop="time">
        <el-date-picker v-model="time" type="datetimerange" range-separator="至" start-placeholder="开始日期"
          end-placeholder="结束日期" @change="changetime" value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:xtxlxy:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="xtxlxyList" @selection-change="handleSelectionChange">
      <el-table-column v-if="tab === 'others'" :key="Math.random()" label="步数" align="center" prop="step" />
      <el-table-column v-if="tab === 'others'" :key="Math.random()" label="心率" align="center" prop="xinlv" />
      <el-table-column v-if="tab === 'others'" :key="Math.random()" label="血氧" align="center" prop="xueyang" />
      <el-table-column v-if="tab === 'others'" :key="Math.random()" label="收缩压" align="center" prop="shousuoya" />
      <el-table-column v-if="tab === 'others'" :key="Math.random()" label="舒张压" align="center" prop="shuzhangya" />
      <el-table-column v-if="tab === 'sleep'" label="醒来次数" align="center" prop="count" show-overflow-tooltip />
      <el-table-column v-if="tab === 'sleep'" label="浅睡时间" align="center" prop="shallow" show-overflow-tooltip />
      <el-table-column v-if="tab === 'sleep'" label="深睡时间" align="center" prop="deep" show-overflow-tooltip />
      <el-table-column label="日期-时间" align="center" prop="newTime" show-overflow-tooltip />
      <el-table-column label="设备名称" align="center" prop="nickName" />
      <el-table-column label="蓝牙mac地址" align="center" prop="deviceId" show-overflow-tooltip />
      <el-table-column label="型号" align="center" prop="equipmentModel" />
      <el-table-column label="使用人" align="center" prop="uname" />
      <el-table-column label="电话" align="center" prop="uphone" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">用户详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改心率,心氧,血压对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listXtxlxy,
    getXtxlxy,
    delXtxlxy,
    addXtxlxy,
    updateXtxlxy,
  } from "@/api/motion";

  export default {
    name: "Xtxlxy",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 心率,心氧,血压表格数据
        xtxlxyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          startTime: null,
          endTime: null,
          val: "",
        },
        selectType: "name",
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        time: [],
        tab: "others",
        uphonesss: ''
      };
    },
    created() {
      // console.log(this.$route.query)
      if (this.$route.query.uphone) {
        this.uphonesss = this.$route.query.uphone
      }
      if (this.$route.query.deviceId) {
        this.selectType = 'deviceId'
        this.queryParams.val = this.$route.query.deviceId
      }
      this.getList();
    },
    methods: {
      handleChangeTab() {
        this.handleQuery();
      },
      changetime(val) {
        if (!val) {
          this.queryParams.startTime = null
          this.queryParams.endTime = null
          return
        }
        this.queryParams.startTime = val[0];
        this.queryParams.endTime = val[1];
      },
      /** 查询心率,心氧,血压列表 */
      getList() {
        let url =
          this.tab === "others" ? "/system/xtxlxy/list" : "/system/sleep/list";
        this.loading = true;
        listXtxlxy({
            ...this.queryParams,
            [this.selectType]: this.queryParams.val,
            uphone: this.uphonesss
          },
          url
        ).then((response) => {
          let data = response.rows.map((item) => {
            item.shallow = Math.abs(Number(item.shallow) / 60).toFixed(2)
            item.deep = Math.abs(Number(item.deep) / 60).toFixed(2)
            return item
          })
          this.xtxlxyList = data;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          step: null,
          kcal: null,
          xinlv: null,
          xueyang: null,
          shousuoya: null,
          shuzhangya: null,
          deviceId: null,
          createdTime: null,
          updatedTime: null,
          delFlag: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.queryParams = {
          pageNum: 1,
          pageSize: 10,
          startTime: null,
          endTime: null,
          val: "",
        }
        this.selectType = 'name'
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.id);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加心率,心氧,血压";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        debugger
        // console.log(row)
        this.$router.push({
          path: '/userdet',
          query: {
            id: row.userId,
            uphone: row.uphone
          }
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.id != null) {
              updateXtxlxy(this.form).then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addXtxlxy(this.form).then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal
          .confirm('是否确认删除心率,心氧,血压编号为"' + ids + '"的数据项？')
          .then(function() {
            return delXtxlxy(ids);
          })
          .then(() => {
            this.getList();
            this.$modal.msgSuccess("删除成功");
          })
          .catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "system/xtxlxy/export", {
            ...this.queryParams,
          },
          `xtxlxy_${new Date().getTime()}.xlsx`
        );
      },
    },
  };
</script>
<style lang="scss" scoped>
  .sleepBtn {
    float: right;
    margin-left: 10px;
  }
</style>
